import os
from . import tools, configs
import random


class CMSGenerator(object):
    cur_dir = os.path.dirname(__file__)
    output_dir = os.path.abspath(os.path.join(os.path.dirname(cur_dir), "output"))

    def __init__(self):
        self._format_raw = '[su_row][su_column size="1/5" center="no" class=""]<img src="/wp-content/uploads' \
                           '/Liquid-Bottle-Filling-Machine{random_num}.jpg"  width="300" height="300" class=' \
                           '"alignnone size-medium wp-image-617" />[/su_column][su_column size="4/5" center="no"' \
                           ' class=""]<h3>{title}</h3><p>{brief_content}</p><div class="alpha-contact"><div ' \
                           'id="MyLiveChatContainer"></div></div>[/su_column][/su_row]\n'
        self._format_cms = \
            """
<item>
    <title>{key_word}</title>
    <dc:creator><![CDATA[NPACK]]></dc:creator>
    <content:encoded><![CDATA[{content}]]></content:encoded>
    <excerpt:encoded><![CDATA[]]></excerpt:encoded>
    <wp:post_date><![CDATA[{random_datetime}]]></wp:post_date>
    <wp:post_date_gmt><![CDATA[{random_datetime}]]></wp:post_date_gmt>
    <wp:comment_status><![CDATA[closed]]></wp:comment_status>
    <wp:ping_status><![CDATA[closed]]></wp:ping_status>
    <wp:status><![CDATA[publish]]></wp:status>
    <wp:post_parent>0</wp:post_parent>
    <wp:menu_order>0</wp:menu_order>
    <wp:post_type><![CDATA[post]]></wp:post_type>
    <wp:is_sticky>0</wp:is_sticky>
    <category domain="category" nicename="filling-and-capping-machine"><![CDATA[Filling And Capping Machine]]></category>
    <wp:postmeta>
        <wp:meta_key><![CDATA[_thumbnail_id]]></wp:meta_key>
        <wp:meta_value><![CDATA[{random_num_cms}]]></wp:meta_value>
    </wp:postmeta>
</item>
"""

    def generate(self, crawl_data):
        assert isinstance(crawl_data, dict), "crawl_data type should be dict, actual: {}".format(type(crawl_data))
        cms_data = ""
        for key_word, val in crawl_data.items():
            random_datetime = tools.random_datetime()[0]
            config_obj = configs.Config()
            random_num1 = random.randint(config_obj.random_num_range_cms["min"], config_obj.random_num_range_cms["max"])
            content = ""
            for raw in val["data"]:
                random_num2 = random.randint(config_obj.random_num_range["min"], config_obj.random_num_range["max"])
                content += self._format_raw.format(title=raw.title, brief_content=raw.brief_content, random_num=random_num2)
            with open(os.path.join(CMSGenerator.output_dir, "{}.txt".format(key_word)), "w", encoding="utf8") as f1:
                f1.write(content)
            cms_data += self._format_cms.format(key_word=key_word, content=content, random_datetime=random_datetime,
                                               random_num_cms=random_num1)
        with open(os.path.join(CMSGenerator.output_dir, "cms.txt"), "w", encoding="utf8") as f2:
            f2.write(cms_data)


if __name__ == "__main__":
    pass